﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelMgr.Models;

namespace HotelMgr.Controllers
{
    [Authorize]
    public class CheckoutController : Controller
    {
        HotelEntities db=new HotelEntities();
        private const string PromoCode = "FREE";
        //
        // GET: /Checkout/AddressAndPayment

        public ActionResult AddressAndPayment()
        {
            return View();
        }
        //
        //POST:/Checkout/AddressAndPayment

        [HttpPost]
        public ActionResult AddressAndPayment(FormCollection values)
        {
            var order = new Order();
            TryUpdateModel(order);
            try
            {
                if(string.Equals(values["PromoCode"],PromoCode,
                    StringComparison.OrdinalIgnoreCase)==false)
                {
                    return View(order);
                }
                else
                {
                    //order.Username = User.Identity.Name;
                    order.CheckOutTime = DateTime.Now;

                    var cart = ShoppingCart.GetCart(this.HttpContext);
                    cart.CreateOrser(order);

                    return RedirectToAction("Complete",
                                            new {id = order.OrderID});
                }
            }
            catch
            {

                return View(order);
            }
        }
        //
        //GET:/Checkout/Complete

        public ActionResult Complete(int id)
        {
            //
            bool isValid = db.Orders.Any(
                o => o.OrderID == id);
                    //&&o.Username == User.Identity.Name);
            if (isValid)
            {
                return View(id);
            }
            else
            {
                return View("Error");
            }
        }
    }
}
